package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.commom.result.Result;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ZhangYK
 * @version 8.0
 * @title RoleController
 * @description
 * @create 2024/7/5 18:06
 */
@Api(tags = "角色接口")
@RestController
@RequestMapping("admin/acl/role")
@CrossOrigin
public class RoleController {

    @Resource
    private RoleService roleService;

    //1、角色列表，分页查询
    // url: `${api_name}/${page}/${limit}`,
    //      method: 'get',
    @ApiOperation("角色条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result pageList(@PathVariable("page") Integer page,
                           @PathVariable("limit") Integer limit,
                            RoleQueryVo roleQueryVo) {
        //1、创建page对象，传递当前页和每页记录数
        Page<Role> pageParam = new Page<>(page, limit);
        //2、调用service方法实现条件分页查询，返回分页对象
        IPage<Role> pageModel = roleService.selectRolePage(pageParam,roleQueryVo);

        return Result.ok(pageModel);
    }

    //2、根据id查询角色
//    url: `${api_name}/get/${id}`,
//    method: 'get'
    @ApiOperation("根据id查询角色")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") Long id) {
        return Result.ok(roleService.getById(id));
    }
    //3、添加角色
    // url: `${api_name}/save`,
    //      method: 'post',
    //      data: role
    @ApiOperation("添加角色")
    @PostMapping("save")
    public Result save(@RequestBody Role role) {
        if (roleService.save(role)) {
            return Result.ok(null);
        }
        return Result.fail(null);
    }
    //4、修改角色
    // url: `${api_name}/update`,
    //      method: 'put',
    //      data: role
    @ApiOperation("修改角色")
    @PutMapping("update")
    public Result update(@RequestBody Role role) {
        if (roleService.updateById(role)) {
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    //5、根据id删除角色
    // url: `${api_name}/remove/${id}`,
    //      method: 'delete'
    @ApiOperation("根据id删除角色")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable("id") Long id) {
        if (roleService.removeById(id)) {
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    //6、批量删除角色
    // url: `${api_name}/batchRemove`,
    //      method: 'delete',
    //      data: ids
    @ApiOperation("批量删除角色")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        roleService.removeByIds(ids);
        return Result.ok(null);
    }

}
